package com.qiguliuxing.dts.merchant.controller;

import com.qiguliuxing.dts.merchant.service.MerchantGoodsService;
import com.qiguliuxing.dts.merchant.service.MerchantOrderService;
import com.qiguliuxing.dts.merchant.support.AuthSupport;
import com.qiguliuxing.dts.merchant.vo.CategorySellVo;
import com.qiguliuxing.dts.merchant.vo.OrderAmtsVo;
import com.qiguliuxing.dts.common.constant.ApiAdminConsts;
import com.qiguliuxing.dts.common.util.ResponseUtils;
import com.qiguliuxing.dts.db.bean.CategorySellAmts;
import com.qiguliuxing.dts.db.bean.DayStatis;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.*;

/**
 * 商户端首页统计接口
 * @author qiguliuxing
 */
@RestController
@RequestMapping("/merchant/dashboard")
@Validated
@Api(tags = ApiAdminConsts.Dashbord.PARENT)
public class MerchantDashbordController {
	private static final Logger logger = LoggerFactory.getLogger(MerchantDashbordController.class);

	// 统计的天数范围，一个月数据
	private static final int STATIS_DAYS_RANG = 30;

	@Autowired
	private MerchantGoodsService merchantGoodsService;
	@Autowired
	private MerchantOrderService merchantOrderService;

	@ApiOperation(value="首页仪表盘信息")
	@GetMapping("")
	public Object info() {
		Integer brandId = AuthSupport.merchantBrandId();
		Long visitTotal = null, goodsTotal = null, salesTotal = null, orderTotal = null;
		if (brandId != null) {
			logger.info("当前登录商户id：{}，获取首页仪表盘统计数据！", brandId);
			visitTotal = merchantGoodsService.merchantVisitCount(brandId);
			goodsTotal = merchantGoodsService.merchantGoodsCount(brandId);
			salesTotal = merchantOrderService.merchantSalesTotal(brandId);
			orderTotal = merchantOrderService.merchantOrderCount(brandId);
		}
		Map<String, Object> data = new HashMap<>();
		data.put("visitTotal", visitTotal == null ? 0L : visitTotal);
		data.put("goodsTotal", goodsTotal == null ? 0L : goodsTotal);
		data.put("salesTotal", salesTotal == null ? 0L : salesTotal);
		data.put("orderTotal", orderTotal == null ? 0L : orderTotal);
		return ResponseUtils.ok(data);
	}

	@ApiOperation(value="首页图表信息")
	@GetMapping("/chart")
	public Object chart() {
		Integer brandId = AuthSupport.merchantBrandId();
		// 近期用户，订单增长量查询
		List<DayStatis> orderCnts = merchantOrderService.recentOrderStatis(STATIS_DAYS_RANG, brandId);

		// 订单情况统计，订单笔数与订单金额
		OrderAmtsVo orderAmts = fetchOrderAmtsVo(orderCnts);

		// 统计总量: 大类销售统计情况
		List<CategorySellAmts> categorySellStatis = merchantOrderService.brandCategorySell(brandId);
		CategorySellVo categorySell = fetchCategorySell(categorySellStatis);

		Map<String, Object> data = new HashMap<>();
		data.put("orderAmts", orderAmts);
		data.put("categorySell", categorySell);

		return ResponseUtils.ok(data);
	}

	/**
	 * 获取订单统计数据
	 * 
	 * @param orderCnts
	 * @return
	 */
	private OrderAmtsVo fetchOrderAmtsVo(List<DayStatis> orderCnts) {
		OrderAmtsVo orderAmts = new OrderAmtsVo();
		int size = 0;
		if (orderCnts != null && orderCnts.size() > 0) {
			size = orderCnts.size();
		}
		String[] dayData = new String[size];
		int[] orderCntData = new int[size];
		BigDecimal[] orderAmtData = new BigDecimal[size];
		for (int i = 0; i < size; i++) {
			dayData[i] = orderCnts.get(i).getDayStr();
			orderCntData[i] = orderCnts.get(i).getCnts();
			orderAmtData[i] = orderCnts.get(i).getAmts();
		}
		orderAmts.setDayData(dayData);
		orderAmts.setOrderAmtData(orderAmtData);
		orderAmts.setOrderCntData(orderCntData);
		return orderAmts;
	}

	/**
	 * 获取大类的销售统计数据
	 * 
	 * @param categorySellData
	 * @return
	 */
	private CategorySellVo fetchCategorySell(List<CategorySellAmts> categorySellData) {
		CategorySellVo categorySell = new CategorySellVo();
		int size = 0;
		if (categorySellData != null && categorySellData.size() > 0) {
			size = categorySellData.size();
		}
		String[] categoryNames = new String[size];
		for (int i = 0; i < size; i++) {
			categoryNames[i] = categorySellData.get(i).getName();
		}
		categorySell.setCategoryNames(categoryNames);
		categorySell.setCategorySellData(categorySellData);
		return categorySell;
	}

}
